package cn.wolfcode.crm.util;


import cn.wolfcode.crm.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Set;

public abstract class UserContext {
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    public static HttpSession getSession() {
        //静态方法 RequestContextHolder.getRequestAttributes()
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes servicletRequestAttributes = (ServletRequestAttributes) requestAttributes;
//        return servicletRequestAttributes.getRequest().getSession();
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }

    public static void setEmployeeAttribute(Employee emp) {
        //共享用户信息
        getSession().setAttribute(EMPLOYEE_IN_SESSION, emp);
    }

    public static Employee getEmployeeAttribute() {
        //获取共享用户信息
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);

    }
    public static Employee getEmployeeInSession() {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        return employee;
    }

    public static void setExpressionAttribute(Set<String> expressions) {
        //共享用户权限信息
        getSession().setAttribute(EXPRESSION_IN_SESSION, expressions);
    }

    public static Set<String> getExpressionAttribute() {
        //获取共享的用户权限信息
        return (Set<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
